package com.khotyn.heresy.service;

import com.khotyn.heresy.bean.Friend;
import com.khotyn.heresy.bean.PictureDetail;
import com.khotyn.heresy.dao.FriendDAO;
import com.khotyn.heresy.dao.PicCommentDAO;
import com.khotyn.heresy.dao.PictureDAO;
import com.khotyn.heresy.dao.ScoreDAO;

/**
 * 图片中心服务类
 * 
 * @author 黄挺
 * 
 */
public class PictureService {
	private PictureDAO pictureDAO;
	private PicCommentDAO picCommentDAO;
	private ScoreDAO scoreDAO;
	private FriendDAO friendDAO;

	/**
	 * 图片中心服务类
	 * 
	 * @param pictureID 图片ID
	 * @param userID 用户ID
	 * @param requestUrl 请求URL
	 * @return 图片详细Bean
	 * @throws Exception 图片访问权限受限
	 */
	public PictureDetail doService(String pictureID, Integer userID, String requestUrl) throws Exception {
		PictureDetail picture = doDBOperate(pictureID, userID, requestUrl);

		// 判断访问者是否拥有访问图片的权限
		if (picture.getFlagLimit() == "private" && (userID == null || picture.getPicOwner().getUserID() != userID)) {
			throw new Exception("此图片属于私人所有，您无法访问");
		} else if (picture.getFlagLimit() == "friend") {
			Friend friend = friendDAO.selectOneFriend(picture.getPicOwner().getUserID(), userID);
			if (userID == null && friend == null)
				throw new Exception("此图片只有所有者的好友才能访问");
		}

		return picture;
	}

	/**
	 * 图片中心数据库操作函数
	 * 
	 * @param pictureID 相册ID
	 * @param userID 用户ID
	 * @param requestUrl 请求URL
	 * @return 图片详细Bean
	 */
	public PictureDetail doDBOperate(String pictureID, Integer userID, String requestUrl) {
		PictureDetail picture = new PictureDetail();
		picture = pictureDAO.selectPicByPicID(pictureID);
		picture.setComments(picCommentDAO.selectPagedCommentByPicID(pictureID, 0, 10));
		picture.setScoreNumber(scoreDAO.selectScoreNumber(pictureID));
		return picture;
	}

	public void setPictureDAO(PictureDAO pictureDAO) {
		this.pictureDAO = pictureDAO;
	}

	public void setPicCommentDAO(PicCommentDAO picCommentDAO) {
		this.picCommentDAO = picCommentDAO;
	}

	public void setScoreDAO(ScoreDAO scoreDAO) {
		this.scoreDAO = scoreDAO;
	}

	public void setFriendDAO(FriendDAO friendDAO) {
		this.friendDAO = friendDAO;
	}
}
